//
//  HomeVC.swift
//  SwfitDemo
//
//  Created by menchao on 2018/9/25.
//  Copyright © 2018年 Test. All rights reserved.
//

import UIKit

class HomeVC: UIViewController,UITableViewDelegate, UITableViewDataSource {
    
    var mytableView:UITableView?
    var dataArray:Array<HomeModel>?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Swift 学习";
        
        let model = HomeModel()
        model.title = "各种控件,布局适配框架snipkit"
        
        let model2 = HomeModel()
        model2.title = "网络及解析"
        
        let model3 = HomeModel()
        model3.title = "线程"
     
        self.dataArray = [model,model2,model3]
        self.initView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    // MARK: -  tableView
    func initView() {
        self.mytableView  = UITableView(frame:CGRect(x:0, y:0, width:self.view.frame.size.width, height:self.view.frame.size.height), style:UITableViewStyle.plain)
        self.mytableView!.delegate = self
        self.mytableView!.dataSource = self
        self.mytableView?.register(HomeTableViewCell.self, forCellReuseIdentifier: "HomeTableViewCell")
        self.view.addSubview(self.mytableView!)
        let footerView = UIView.init(frame: CGRect(x:0, y:0, width:self.view.frame.size.width, height:0.5))
        footerView.backgroundColor = UIColor.init(red: 217/255.0, green: 217/255.0, blue: 217/255.0, alpha: 1)
        self.mytableView?.tableFooterView = footerView
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return (self.dataArray?.count)!;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath)
        let newCell = cell as? HomeTableViewCell;
        newCell?.updateCellData(model: (self.dataArray?[indexPath.row])!)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 80
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        NSLog("%d,%d", indexPath.section,indexPath.row)
        var vc:UIViewController?
        if(indexPath.row == 0){
             vc = ChildVC()
         }else if(indexPath.row == 1){
             vc = NetworkVC()
        }else if(indexPath.row == 2){
             vc = ThreadVC()
         }
        self.navigationController?.pushViewController(vc!, animated: true)
        self.navigationController?.hidesBottomBarWhenPushed = true
        
    }
    
}
